/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.domain.bo;

import com.comac.ins.common.core.domain.model.LoginUser;
import com.comac.ins.common.satoken.utils.LoginHelper;
import com.comac.ins.isfd.constant.enums.IsfdImportDataOperationEnum;
import com.comac.ins.isfd.constant.enums.IsfdImportMethodEnum;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * mongo导入记录字段
 *
 * @author Lion Li
 * @date 2024-11-27
 */
@Data
public class IsfdDatabaseDataMongoImportRecordBo {

    /**
     * 导入主表id
     */
    private Long id;

    /**
     * 导入方法
     */
    private String importMethod;

    /**
     *
     * 删除标记
     */
    @JsonProperty("delete_flag")
    private int deleteFlag;

    /**
     *
     * 操作
     */
    private String operation;

    private Long createBy;

    private Long updateBy;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Date createTime;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Date updateTime;



    public void setImportMethod(String importMethod) {
        if (!IsfdImportMethodEnum.isValid(importMethod)) {
            throw new IllegalArgumentException("非法的导入方法: " + importMethod);
        }
        this.importMethod = importMethod;
    }

    public void setDeleteFlag(int deleteFlag) {
        if (deleteFlag != 0 && deleteFlag != 1) {
            throw new IllegalArgumentException("deleteFlag 只能是 0 或 1");
        }
        this.deleteFlag = deleteFlag;
    }

    public void setOperation(String operation) {
        if(!IsfdImportDataOperationEnum.isValid(operation)) {
            throw new IllegalArgumentException("非法的操作: " + operation);
        }
        this.operation = operation;
    }

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", this.id);
        map.put("importMethod", this.importMethod);
        map.put("delete_flag", this.deleteFlag);
        map.put("operation", this.operation);
        map.put("createBy", this.createBy);
        map.put("updateBy", this.updateBy);
        map.put("createTime", this.createTime);  // 👈 保留为 java.util.Date
        map.put("updateTime", this.updateTime);  // 👈 保留为 java.util.Date
        return map;
    }

    public static IsfdDatabaseDataMongoImportRecordBo fromMap(Map<String, Object> map) {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.convertValue(map, IsfdDatabaseDataMongoImportRecordBo.class);
    }

    public static IsfdDatabaseDataMongoImportRecordBo createMongoImportRecord(IsfdDatabaseDataImportRecordBo recordBo) {
        IsfdDatabaseDataMongoImportRecordBo mongoImportRecordBo = new IsfdDatabaseDataMongoImportRecordBo();
        mongoImportRecordBo.setId(recordBo.getId());
        mongoImportRecordBo.setImportMethod(recordBo.getImportMethod());
        mongoImportRecordBo.setOperation(IsfdImportDataOperationEnum.CREATED.getValue());
        mongoImportRecordBo.setCreateBy(recordBo.getCreateBy());
        mongoImportRecordBo.setUpdateBy(recordBo.getUpdateBy());
        mongoImportRecordBo.setCreateTime(recordBo.getCreateTime());
        mongoImportRecordBo.setUpdateTime(recordBo.getUpdateTime());
        return mongoImportRecordBo;
    }

    public static IsfdDatabaseDataMongoImportRecordBo createMongoImportRecord(IsfdDatabaseDataMongoImportRecordBo recordBo) {
        IsfdDatabaseDataMongoImportRecordBo mongoImportRecordBo = new IsfdDatabaseDataMongoImportRecordBo();
        mongoImportRecordBo.setId(recordBo.getId());
        mongoImportRecordBo.setImportMethod(recordBo.getImportMethod());
        mongoImportRecordBo.setOperation(IsfdImportDataOperationEnum.CREATED.getValue());
        mongoImportRecordBo.setCreateBy(recordBo.getCreateBy());
        mongoImportRecordBo.setUpdateBy(recordBo.getUpdateBy());
        mongoImportRecordBo.setCreateTime(recordBo.getCreateTime());
        mongoImportRecordBo.setUpdateTime(recordBo.getUpdateTime());
        return mongoImportRecordBo;
    }

    public static IsfdDatabaseDataMongoImportRecordBo updateMongoImportRecord(IsfdDatabaseDataMongoImportRecordBo currentRecord) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        currentRecord.setUpdateBy(loginUser.getUserId());
        currentRecord.setUpdateTime(new Date());
        currentRecord.setOperation(IsfdImportDataOperationEnum.UPDATED.getValue());
        return currentRecord;
    }

    public static IsfdDatabaseDataMongoImportRecordBo updateMongoImportRecordDeleteFlag(IsfdDatabaseDataMongoImportRecordBo currentRecord, Integer value) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        currentRecord.setUpdateBy(loginUser.getUserId());
        currentRecord.setUpdateTime(new Date());
        currentRecord.setDeleteFlag(value);
        String operation = value == 0
            ? IsfdImportDataOperationEnum.UPDATED.getValue()
            : IsfdImportDataOperationEnum.DELETED.getValue();
        currentRecord.setOperation(operation);
        return currentRecord;
    }

}
